package bookmanagementsystem.dao;

import bookmanagementsystem.model.ReturningRecord;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.sql.Timestamp;

public class ReturningDAO {
    private static final String URL = "jdbc:mysql://localhost:3306/book_management_system";
    private static final String USERNAME = "root";
    static final String PASSWORD = "root";

    // 获取数据库连接的私有方法，方便统一管理连接创建和异常处理
    private static Connection getConnection() {
        try {
            return DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("数据库连接失败，请检查数据库配置和服务是否正常启动。", e);
        }
    }

    // 添加归还记录到数据库
    public boolean addReturning(ReturningRecord returningRecord) {
        String sql = "INSERT INTO returning_table (record_id, user_id, book_id, returning_time) VALUES (?,?,?,?)";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            preparedStatement.setString(1, returningRecord.getRecordID());
            preparedStatement.setString(2, returningRecord.getUserID());
            preparedStatement.setString(3, returningRecord.getBookID());
            preparedStatement.setTimestamp(4, returningRecord.getReturningTime());

            int rowsAffected = preparedStatement.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("添加归还记录到数据库失败，原因: " + e.getMessage());
            return false;
        }
    }

    // 根据用户ID查询该用户的所有归还记录
    public List<ReturningRecord> queryReturningRecordsByUser(String userID) {
        List<ReturningRecord> returningRecords = new ArrayList<>();
        String sql = "SELECT * FROM returning_table WHERE user_id = ?";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {


            preparedStatement.setString(1, userID);


            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    String recordID = resultSet.getString("record_id");
                    String bookID = resultSet.getString("book_id");
                    Timestamp returningTime = resultSet.getTimestamp("returning_time");

                    ReturningRecord returningRecord = new ReturningRecord(recordID, userID, bookID, returningTime);
                    returningRecords.add(returningRecord);
                }
            }

            return returningRecords;
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("根据用户ID查询所有归还记录失败，原因: " + e.getMessage());
            return returningRecords;
        }
    }
}